package com.yuke.cloud.service.tmc.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.base.dto.MsgNotificationBody;
import com.yuke.cloud.common.core.mq.AbstractMessageListener;
import com.yuke.cloud.service.tmc.controller.frontend.WebSocketController;
import com.yuke.cloud.service.tmc.service.FeedService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.user.SimpUser;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Configuration
public class TmcMessageListener extends AbstractMessageListener {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private SimpUserRegistry userRegistry;
    @Resource
    private WebSocketController webSocketController;
    @Resource
    private FeedService feedService;


    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        MsgNotificationBody msgNotificationBody = new MsgNotificationBody();
        Object bizObj = messageConverter.fromMessage(message);  //json字符串
        logger.info("get message success:" + bizObj.toString());
        JSONObject jsonObject = JSON.parseObject(bizObj.toString());

        //发送单人
//        String msgType = jsonObject.getString("MessageType");
//        String name = jsonObject.getString("wareHouseName");

        //发送多人
        String msgType = jsonObject.getString("MessageType");
        if (msgType == null || msgType.equals("")) {
            return;
        }

        if (msgType != null) {
            if (msgType.equals("needPrint")) {
                String WarehouseIds = jsonObject.getString("WarehouseIds");
                if (WarehouseIds == null || WarehouseIds.equals("")) {
                    return;
                }
                log.info("name : " + WarehouseIds + ",msgType:" + msgType);
                String[] split = WarehouseIds.split(",");

                for (int i = 0; i < split.length; i++) {
                    msgNotificationBody.setMsgType("orderMsg");
                    System.out.println(Long.valueOf(split[i]));
                    msgNotificationBody.setWarehouseId(Long.valueOf(split[i]));
                    msgNotificationBody.setTitle("订单提醒");
//                    msgNotificationBody.setContent("你有新的订单需要发货");
                    String msgContent = jsonObject.getString("body");
                    msgNotificationBody.setContent(msgContent);
                    msgNotificationBody.setTime(new Date());
                    feedService.broadcastMsg("/topic/queue/orderMsg", JSON.toJSON(msgNotificationBody).toString());
                }
            } else if (msgType.equals("PrintOrder")) {

            } else if (msgType.equals("shopReview")) {
                //通知店铺管理员，有店铺申请需要审核
                log.info("--- shopReview MessageType：{} ---", msgType);
                msgNotificationBody.setMsgType("shopMsg");
                msgNotificationBody.setTitle("店铺审核");
                msgNotificationBody.setContent("你有新的店铺申请审核！");
                msgNotificationBody.setTime(new Date());
                feedService.broadcastMsg("/topic/queue/shopMsg", JSON.toJSON(msgNotificationBody).toString());
            } else if (msgType.equals("shopGoodsReview")) {
                //通知店铺管理员，有商品申请需要审核
                log.info("--- shopGoodsReview MessageType：{} ---", msgType);
                msgNotificationBody.setMsgType("shopMsg");
                msgNotificationBody.setTitle("商品审核");
                msgNotificationBody.setContent("你有新的商品申请审核！");
                msgNotificationBody.setTime(new Date());
                feedService.broadcastMsg("/topic/queue/shopMsg", JSON.toJSON(msgNotificationBody).toString());
            } else if (msgType.equals("newTransferApplication")) {
                //通知仓库管理员，有新调货申请需要等待
                String WarehouseIds = jsonObject.getString("WarehouseId");
                if (WarehouseIds == null || WarehouseIds.equals("")) {
                    return;
                }
                msgNotificationBody.setMsgType("warehouseMsg");
                msgNotificationBody.setWarehouseId(Long.valueOf(WarehouseIds));
                msgNotificationBody.setTitle("调货提醒");
                msgNotificationBody.setContent("你有新的调货请求！");
                msgNotificationBody.setTime(new Date());
                feedService.broadcastMsg("/topic/queue/warehouseMsg", JSON.toJSON(msgNotificationBody).toString());

            }
        }
    }

    private Boolean onLine(String userName) {
        log.info("当前在线人数：" + userRegistry.getUserCount());
        for (SimpUser user : userRegistry.getUsers()) {
            log.info("用户 ：" + user);
            log.info("userName : " + user.getName() + ", userSession : " + user.getSessions().toString());
            if (userName.equals(user.getName())) {
                return true;
            }
        }
        return false;
    }
}
